package com.example.Util;


import java.util.HashMap;
import java.util.Map;

public class MessageUtil {

    public static MessageUtil messageUtil;

    public static Map<Short, String> codeMessageMap = new HashMap<>();
    public static Map<Integer, String> CodeErrorMap = new HashMap<>();

    public void setCodeMessageMap() {
        codeMessageMap.put((short) DevComm.DETECT_FINGER, "识别到正常指纹");
        codeMessageMap.put((short) DevComm.NO_DETECT_FINGER, "识别到非正常指纹");
        codeMessageMap.put((short) DevComm.NEED_RELEASE_FINGER, "放开手指");
        codeMessageMap.put((short) DevComm.NEED_FIRST_SWEEP, "第一次录入");
        codeMessageMap.put((short) DevComm.NEED_SECOND_SWEEP, "第二次录入");
        codeMessageMap.put((short) DevComm.NEED_THIRD_SWEEP, "第三次录入");
        codeMessageMap.put((short) DevComm.CMD_CLEAR_TEMPLATE_CODE, "删除成功");
        codeMessageMap.put((short) DevComm.CMD_CLEAR_ALLTEMPLATE_CODE, "删除全部指纹成功");
        codeMessageMap.put((short) DevComm.CMD_FP_CANCEL_CODE, "取消操作成功");
        codeMessageMap.put((short) DevComm.CMD_UP_IMAGE_CODE, "接受指纹图像成功");
        codeMessageMap.put((short) DevComm.CMD_WRITE_TEMPLATE_CODE, "添加成功");
    }
    public void setErrorMap(){
        CodeErrorMap.put(DevComm.ERR_VERIFY, "错误指纹");
        CodeErrorMap.put(DevComm.ERR_IDENTIFY, "找不到此指纹");
        CodeErrorMap.put(DevComm.ERR_EMPTY_ID_NOEXIST, "Empty Template no Exist");
        CodeErrorMap.put(DevComm.ERR_BROKEN_ID_NOEXIST, "Broken Template no Exist");
        CodeErrorMap.put(DevComm.ERR_TMPL_NOT_EMPTY, "Template of this ID Already Exist");
        CodeErrorMap.put(DevComm.ERR_TMPL_EMPTY, "This Template is Already Empty");
        CodeErrorMap.put(DevComm.ERR_INVALID_TMPL_NO, "Invalid Template No");
        CodeErrorMap.put(DevComm.ERR_ALL_TMPL_EMPTY, "All Templates are Empty");
        CodeErrorMap.put(DevComm.ERR_DUPLICATION_ID, "Duplicated ID : ");
        CodeErrorMap.put(DevComm.ERR_BAD_QUALITY, "Bad Quality Image");
        CodeErrorMap.put(DevComm.ERR_SMALL_LINES, "Small line Image");
        CodeErrorMap.put(DevComm.ERR_TOO_FAST, "Too fast swiping");
        CodeErrorMap.put(DevComm.ERR_TIME_OUT, "Time Out");
        CodeErrorMap.put(DevComm.ERR_GENERALIZE, "Fail to Generalize");
        CodeErrorMap.put(DevComm.ERR_NOT_AUTHORIZED, "Device not authorized.");
        CodeErrorMap.put(DevComm.ERR_EXCEPTION, "Exception Error ");
        CodeErrorMap.put(DevComm.ERR_INVALID_TMPL_DATA, "Invalid Template Data");
        CodeErrorMap.put(DevComm.ERR_MEMORY, "Memory Error ");
        CodeErrorMap.put(DevComm.ERR_INVALID_PARAM, "Invalid Parameter");
        CodeErrorMap.put(DevComm.ERR_NO_RELEASE, "No Release Finger Fail");
        CodeErrorMap.put(DevComm.ERR_INTERNAL, "Internal Error.");
        CodeErrorMap.put(DevComm.ERR_FP_CANCEL, "Canceled.");
        CodeErrorMap.put(DevComm.ERR_INVALID_OPERATION_MODE, "Invalid Operation Mode");
        CodeErrorMap.put(DevComm.ERR_NOT_SET_PWD, "Password was not set.");
        CodeErrorMap.put(DevComm.ERR_ADJUST_SENSOR, "Failed to adjust sensor.");
        CodeErrorMap.put(DevComm.ERR_FP_NOT_DETECTED, "Failed to adjust sensor.");

    }

    public MessageUtil() {
        setCodeMessageMap();
        setErrorMap();
    }

    public static MessageUtil getInstance() {
        if (messageUtil == null) {
            synchronized (MessageUtil.class) {
                if (messageUtil == null) {
                    messageUtil = new MessageUtil();
                }
            }
        }
        return messageUtil;
    }

    public String getCodeMessage(Short code){
        return codeMessageMap.getOrDefault(code,"");
    }
    public String GetErrorMsg(short p_wErrorCode) {
        String w_ErrMsg = "操作失败: ";
        int errorCode = p_wErrorCode & 0xFF;
        w_ErrMsg +=  CodeErrorMap.getOrDefault(errorCode,"未知错误...");
        return w_ErrMsg;
    }


}
